<template>
  <!-- 成本法 -->
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('投资时间')" prop="investmentTime">
            <el-date-picker
              v-model="form.investmentTime"
              clearable
              disabled
              style="width: 200px"
              type="date"
              value-format="yyyy-MM-dd"
              :placeholder="$t('投资时间')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('总投资成本') + $t('金额单位')" prop="totalInvestmentCost">
            <Money v-model="form.totalInvestmentCost" :unit="'w'" disabled :placeholder="$t('总投资成本')" @change="calculation" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('回款本金') + $t('金额单位')" prop="collectionIncome">
            <Money v-model="form.collectionIncome" :unit="'w'" disabled :placeholder="$t('回款本金')" @change="calculation" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('剩余投资成本') + $t('金额单位')" prop="remainingInvestmentCost">
            <Money v-model="form.remainingInvestmentCost" :unit="'w'" disabled :placeholder="$t('剩余投资成本')" @change="calculation" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('最新持股比例') + '(%)'" prop="shareholdingRatio">
            <Money v-model="form.shareholdingRatio" disabled :placeholder="$t('最新持股比例')" @change="calculation" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('项目估值') + $t('金额单位')" prop="dealValuation">
            <Money v-model="form.dealValuation" :unit="'w'" disabled :placeholder="$t('项目估值')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('公司整体估值') + $t('金额单位')" prop="overallValuationCompany">
            <Money v-model="form.overallValuationCompany" :unit="'w'" :decimal-places="0" disabled :placeholder="$t('公司整体估值')" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('备注')" prop="remark">
            <el-input v-model="form.remark" type="textarea" maxlength="2000" show-word-limit :placeholder="$t('备注')" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <el-button @click="cancel">{{ $t('取消') }}</el-button>
      <el-button :type="'primary'" @click="submitForm">{{ $t('保存') }}</el-button>
    </div>
  </div>
</template>

<script>
import { getValuationMethodLawCosts } from '@/api/deal/analysisValuation/dealAnalysisValuation'
import { ruoyiFloat } from '@/utils/ruoyi'

export default {
  components: {},
  props: {},
  data() {
    return {
      // 遮盖层
      loading: false,
      form: {},
      rules: {
        investmentTime: [{ required: true, message: this.$t('必填信息'),	trigger: ['change', 'blur'] }],
        totalInvestmentCost: [{ required: true, message: this.$t('必填信息'),	trigger: ['change', 'blur'] }],
        collectionIncome: [{ required: true, message: this.$t('必填信息'),	trigger: ['change', 'blur'] }],
        remainingInvestmentCost: [{ required: true, message: this.$t('必填信息'),	trigger: ['change', 'blur'] }],
        shareholdingRatio: [{ required: true, message: this.$t('必填信息'),	trigger: ['change', 'blur'] }],
        dealValuation: [{ required: true, message: this.$t('必填信息'),	trigger: ['change', 'blur'] }],
        overallValuationCompany: [{ required: true, message: this.$t('必填信息'),	trigger: ['change', 'blur'] }]
      }
    }
  },
  watch: {},
  created() {
  },
  methods: {
    init(data) {
      this.loading = true
      getValuationMethodLawCosts(data.dealId).then(response => {
        this.form = response.data
        this.calculation()
        this.loading = false
      })
    },
    edit(data) {
      this.form = data
    },
    /** 自动计算 **/
    calculation() {
      // 总投资成本   totalInvestmentCost
      // 回款本金     collectionIncome
      // 剩余投资成本  remainingInvestmentCost  总投资成本(万) - 剩余投资成本(万) ，最小值为0，如果算出来负数也展示0
      // 项目估值      dealValuation            = 剩余投资成本(万)
      // 公司整体估值  overallValuationCompany    剩余投资成本(万) / 最新持股比例(%)  * 100
      this.$nextTick(() => {
        this.form.remainingInvestmentCost = this.formatNum(this.form.totalInvestmentCost) - this.formatNum(this.form.collectionIncome)
        this.form.dealValuation = this.formatNum(this.form.remainingInvestmentCost)
        // 百分比 / 100 省略直接乘 10000
        this.form.overallValuationCompany = this.formatNum(this.form.remainingInvestmentCost / this.form.shareholdingRatio * 100)
        // 只保留整数万
        // if (this.form.overallValuationCompany && this.form.overallValuationCompany > 10000) {
        //   var temp = this.form.overallValuationCompany
        //   temp = temp.toString().replaceAll(',', '')
        //   temp = temp.substring(0, temp.lastIndexOf('.'))
        //   temp = temp.substring(0, temp.length - 4) + '0000'
        //   this.form.overallValuationCompany = temp
        // }
      })
    },
    /** 格式化金额, 保留精度及小数 **/
    formatNum(num) {
      if (num === null || num === undefined || isNaN(num)) {
        num = 0
      }
      return ruoyiFloat().formatNum(Number(num).toFixed(4))
    },
    /** 取消 **/
    cancel() {
      this.$parent.$parent.methodLawCostsOpen = false
    },
    /** 提交 **/
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.$emit('lawCostsAddMethod', this.form)
          this.cancel()
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
